package com.jilian.powerstation.modul.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jilian.powerstation.R;
import com.jilian.powerstation.common.dto.DevicesDto;
import com.jilian.powerstation.common.dto.PcsInfoDto;
import com.jilian.powerstation.listener.CustomItemClickListener;
import com.jilian.powerstation.utils.ProUtils;

import java.util.List;

/**
 * 逆变器列表
 */
public class InverDataAdapter extends RecyclerView.Adapter<InverDataAdapter.ESSListHolder> {

    private List<DevicesDto> mDatas;
    private Context context;
    private CustomItemClickListener listener;


    public InverDataAdapter(List<DevicesDto> mDatas, Context context, CustomItemClickListener listener) {
        this.mDatas = mDatas;
        this.context = context;
        this.listener = listener;

    }

    @NonNull
    @Override
    public ESSListHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ESSListHolder(LayoutInflater.from(context).inflate(R.layout.item_inver_data, viewGroup, false), listener);
    }

    @Override
    public void onBindViewHolder(@NonNull ESSListHolder holder, int position) {
        DevicesDto dto = mDatas.get(position);
        if (dto.getBcmuPhoto()==null){
            holder.tvName.setText("Inverter" + ( + 1));
            holder.tvMsgOne.setText("Rated power:");
            holder.tvMsgTwo.setText("Power now:");
            holder.tvOne.setText(mDatas.get(position).getRatePower() + " kW");//额定功率
            holder.tvTwo.setText(mDatas.get(position).getNowChargePower() + " W");//
            Glide.with(context).
                    load(mDatas.get(position).getPcsPhoto())
                    .into(holder.ivHead);//在RequestBuilder 中使用自定义的ImageViewTarge
        }else {
            holder.tvName.setText("Battery" + (mDatas.get(position).getId() + 1));
            holder.tvMsgOne.setText("Charging power:");
            holder.tvMsgTwo.setText("SOC:");
            holder.tvOne.setText(mDatas.get(position).getPower() + " W");//充电功率
            holder.tvTwo.setText(ProUtils.getValueOne(mDatas.get(position).getSoc() + "", 0.1F) + " %");//当前电量

            Glide.with(context).
                    load(mDatas.get(position).getBcmuPhoto())
                    .into(holder.ivHead);//在RequestBuilder 中使用自定义的ImageViewTarge
        }



    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class ESSListHolder extends RecyclerView.ViewHolder {

        private ImageView ivHead;
        private TextView tvName;
        private TextView tvOne;
        private TextView tvTwo;
        private TextView tvMsgOne;
        private TextView tvMsgTwo;


        public ESSListHolder(@NonNull View itemView, CustomItemClickListener listener) {
            super(itemView);

            ivHead = (ImageView) itemView.findViewById(R.id.iv_head);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvOne = (TextView) itemView.findViewById(R.id.tv_one);
            tvTwo = (TextView) itemView.findViewById(R.id.tv_two);
            tvMsgOne = (TextView) itemView.findViewById(R.id.tv_item_msg1);
            tvMsgTwo = (TextView) itemView.findViewById(R.id.tv_item_msg2);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(v, getAdapterPosition());
                    }
                }
            });


        }
    }
}
